package com.bcw.springtest.javabeans;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.stream.Stream;

public class JavaBeanTest {

    public static void main(String[] args) throws IntrospectionException {
        // 要找的类（通过注解等方式）
        Person person = new Person();

        // beanInfo可以理解为类的描述，有writeable、readable方法和属性
        BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
        PropertyDescriptor[]  propertyDescriptors = beanInfo.getPropertyDescriptors();
        Stream.of(propertyDescriptors).forEach(p->{
            if (p.getName().equals("name")){
                Method method = p.getWriteMethod();
                try {
                    method.invoke(person, "wx123");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println(beanInfo);
    }

}
